//index.js
//获取应用实例
var app = getApp()
var tools = require('../../js/tools.js')
function conver(s) {
  return s < 10 ? '0' + s : s;
}


Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    NoticeBar: true,//通知栏：登录成功
    publishloading: false,//发布的loading图标是否显示
    ifpublish: false,
    ifshowtap:false,
    //轮播模块
    cardCur: 0,
    swiperList: [{
      id: 0,
      type: 'image',
      url: 'https://images.pexels.com/photos/3401900/pexels-photo-3401900.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260'
    }, {
      id: 1,
      type: 'image',
      url: 'https://images.pexels.com/photos/1406282/pexels-photo-1406282.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
    }, {
      id: 2,
      type: 'image',
      url: 'shou.jpg'
    },],
    // info:[[{
    //   groupnum:'16555444222',
    //   event:'fasdfxx9955sdfasd',//事件号
    //   title:'标题',
    //   deadline:'2099-12-31 12:59:59',
    //   time:'2020-01-20 12:28:35.681776'
    // },{
    //   groupnum:'19999999992',
    //   event:'sasdfxxasdfsdfasd',//事件号
    //   title:'标题2',
    //   deadline:'2099-12-31 12:59:59',
    //   time:'2020-01-20 12:28:35.681776'
    // }],[{
    //   groupnum:'16555444222',
    //   event:'fasdfxx9955sdfasd',//事件号
    //   title:'标题',
    //   deadline:'2099-12-31 12:59:59',
    //   time:'2020-01-19 12:28:35.681776'
    // }]]
    info: [{
      groupnum: '16555444222',
      event: 'fasdfxx9955sdfasd',//事件号
      title: '标题',
      deadline: '2099-12-31 12:59:59',
      time: '2020-01-20 12:29:35.681776'
    }, {
      groupnum: '19999999992',
      event: 'sasdfxxasdfsdfasd',//事件号
      title: '标题2',
      deadline: '2099-12-31 12:59:59',
      time: '2020-01-20 12:29:35.681776'
    }, {
      groupnum: '16555444222',
      event: 'fasdfxx9955sdfasd',//事件号
      title: '标题',
      deadline: '2099-12-31 12:59:59',
      time: '2020-01-15 12:28:35.681776'
    }, {
      groupnum: '16555444222',
      event: 'fasdfxx9955sdfasd',//事件号
      title: '标题',
      deadline: '2099-12-31 12:59:59',
      time: '2020-01-20 12:29:35.681776'
    },]
  },

  // onShow: function () {
  //   var that = this
  //   wx.getStorage({
  //     key: 'admin',
  //     success(res) {
  //       if (res.data > 0) {
  //         that.setData({
  //           ifpublish: true,
  //         })
  //       }
  //     }
  //   })
  // },

  onLoad: function () {
    var that = this
    wx.request({
      url: 'https://localhost:8080/setSlidingPicture',
      success(res) {
        var rs = res.data
        var seter = that.data.swiperList
        var j = 0;
        if (rs.length == 3) {
          for (var i of rs) {
            if (i == '') {
              j++;
            }
            else {
              seter[j].url = i
              j++
            }
          }
          that.setData({
            swiperList: seter
          })
        }
        else {
          var seter = []
          for (var i of rs) {
            var object = {
              id: j,
              type: 'image',
              url: i
            }
            seter.push(object)
            j++
          }
          that.setData({
            swiperList: seter
          })
        }
      },
      fail(res){
        wx.showToast({
          title:'网络连接失败,本喵需要连接网络后重启重试',
          icon:'none',
          duration: 100000
        })
      }
    })
    this.towerSwiper('swiperList');    // 初始化towerSwiper 传已有的数组名即可
    var that = this//将this先进行赋值，使得this的实例可以作为本函数的局部变量，因此可以让回调函数里面也能访问
    //检查是否是第一登录
    // wx.getStorage({
    //   key: 'Landingtime',
    //   success(res) {
    //     // console.log("登录时间信息存在，正在检查是否过期..")
    //     // tools.gettimestamp()
    //     var date = tools.convertDateFromString(res.data)
    //     if (tools.comparetime(date) == 0) {
    //       //已经过期
    //       wx.navigateTo({
    //         url: '../../pages/landing/landing',
    //         success: function (res) {
    //           console.log(res)
    //         },
    //         fail: function (res) {
    //           wx.showToast({
    //             title: '调用登陆失败',
    //             icon: 'loading',
    //             duration: 1000
    //           })
    //         }
    //       })
    //     }
    //     else {
    //       console.log('set data')
    //       that.setData({
    //         NoticeBar: false
    //       })
    //     }
    //   },
    //   fail(res) {//此时为第一次登陆
    //     wx.navigateTo({
    //       url: '../../pages/landing/landing',
    //       success: function (res) {
    //         console.log(res)
    //       },
    //       fail: function (res) {
    //         wx.showToast({
    //           title: '调用登陆失败',
    //           icon: 'loading',
    //           duration: 1000
    //         })
    //       }
    //     })
    //   }
    // }),
    //   wx.getStorage({
    //     key: 'pwdWithSalt',
    //     success(res) {
    //       console.log("盐存在")
    //     },
    //     fail(res) {//此时为第一次登陆
    //       wx.navigateTo({
    //         url: '../../pages/landing/landing',
    //         success: function (res) {
    //           console.log(res)
    //         },
    //         fail: function (res) {
    //           wx.showToast({
    //             title: '调用登陆失败',
    //             icon: 'loading',
    //             duration: 1000
    //           })
    //         }
    //       })
    //     }
    //   })
  },

  onShow: function () {
    var that=this
    if (app.globalData.ifshowthetap) {
      wx.request({
        url: 'https://localhost:8080/set_tap_image',
        success(res) {
          that.setData({
            url_tap: res.data,
            ifshowtap: true
          })
        }
      })
    }
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          that.setData({
            NoticeBar: false
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })

    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    var date = myDate.getDate();
    var today = year + '-' + conver(month) + "-" + conver(date)
    var yesterday = year + '-' + conver(month) + "-" + conver(date - 1)
    var the_day_before_yesterda = year + '-' + conver(month) + "-" + conver(date - 2)
    this.setData({
      today: today,
      yesterday: yesterday,
      the_day_before_yesterda: the_day_before_yesterda
    })
    var that = this
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/geteventinfo',
          data: {
            pwdWithSalt: res.data
          },
          success(res) {
            tools.strTodate(res.data)
            var info = tools.cutting(res.data)
            info = tools.Standardization(info)
            var info = tools.sortBydate(info)
            that.setData({
              info: info
            })
          }
        })
      }
    })
    wx.getStorage({
      key: 'admin',
      success(res) {
        if (res.data > 0) {
          that.setData({
            ifpublish: true,
          })
        }
      }
    })
  },

  DotStyle(e) {
    this.setData({
      DotStyle: e.detail.value
    })
  },

  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },

  Bdeal: function (e) {
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          wx.navigateTo({
            url: '../../pages/dealmain/dealmain?event=' + e.currentTarget.dataset.item
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  Badd: function () {
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          wx.navigateTo({
            url: '../../pages/add/add'
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  landingcheck: function () {
    wx.request({
      url: 'https://localhost:8080/test/', //仅为示例，并非真实的接口地址
      data: {
        pwdWithSalt: app.globalData.pwdWithSalt
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
      }
    })
  },

  Bpublish: function () {
    var that = this
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          wx.vibrateShort()//震动
          that.setData({
            publishloading: true
          })
          wx.navigateTo({
            url: '../../pages/publish/publish',
            success: function () {
              that.setData({
                publishloading: false
              })
            }
          })
        }
      },
      fail(res) {//此时为第一次登陆
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  chacha:function(){
    this.setData({
      ifshowtap:false
    })
    app.globalData.ifshowthetap=false
  },

  imgbind:function(){
    wx.navigateTo({
      url: 'education',
    })
  }
  // Bgetpicture: function () {
  //   wx.chooseImage({
  //     count: 1,
  //     sizeType: ['compressed'],
  //     sourceType: ['album', 'camera'],
  //     success(res) {
  //       // tempFilePath可以作为img标签的src属性显示图片
  //       const tempFilePaths = res.tempFilePaths
  //       console.log(tempFilePaths)
  //       wx.uploadFile({
  //         url: 'https://localhost:8080/file/', //仅为示例，非真实的接口地址
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           'user': 'test'
  //         },
  //         success(res) {
  //           const data = res.data
  //           //do something
  //         },
  //         fail(res) {
  //           console.log(res)
  //         }
  //       })

  //     }
  //   })
  // }

})
